package com.xpgk.mvc.productSet;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.codeSet.Code;

public class ProductSetController extends BaseController{
	
	private ProductSetService productSetService;
	
	public void index() {
		render("productSet.html");
	}
	
	/*
	 * 更该产品状态
	 */
	@Before(Tx.class)
	public void productListstruts(){
		String ids = getPara("ids");
		Product product = Product.dao.findById(ids);
		product.set(Product.column_status,"0");
		boolean result = product.update();
		renderJson(result);
	}
	
	/**
	 * 产品列表
	 */
	public void productList(){
		String json = paging(splitPage, Product.sqlId_splitPageSelect, Product.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	/**
	 * 跳转至添加产品
	 */
	public void toAddProduct(){
		String typeIds = getPara("ids");
		String sql = "SELECT proBh FROM `sb_product` WHERE typeIds='"+typeIds+"' ORDER BY proBh DESC";
		List<Record> find = Db.find(sql);
		if(find.size()!=0){
			String proBh = find.get(0).get("proBh").toString();
			Integer proBhnumber = Integer.parseInt(proBh.replaceAll("[a-zA-Z]","" ))+1;
			String kk = proBh.substring(0,proBh.length()-proBhnumber.toString().length())+proBhnumber.toString();
			setAttr("typename",kk);
		}
		setAttr("code", Code.cacheGet(ids));
		setAttr("servicePart", Code.cacheGetChild(Code.fwbw_ids));
		render("addProduct.html");
	}
	
	/**
	 * 添加产品
	 */
	@Before(ProductValidator.class)
	public void addProduct(){
		List<ProductExperience> pes = getModels(ProductExperience.class);
		Product product = getModel(Product.class);
		RenderBean renderBean = productSetService.addProduct(product, pes,getCUserIds(),getCUserOrganIds());
		renderJson(renderBean);
	}
	
	/**
	 * 跳转至编辑
	 */
	public void toUpProduct(){
		Product findById = Product.dao.findById(ids);
		setAttr("product", Product.dao.findById(ids));
		render("upProduct.html");
	}
	
	/**
	 * 编辑
	 */
	@Before(ProductValidator.class)
	public void upProduct(){
		List<ProductExperience> pes = getModels(ProductExperience.class);
		Product product = getModel(Product.class);
		RenderBean renderBean = productSetService.upProduct(product, pes,getCUserIds(),getCUserOrganIds());
		renderJson(renderBean);
	}

}
